package com.wc.codeforces.二分.Best_Price;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.StringTokenizer;

/**
 * @Author congge
 * @Date 2025/1/3 20:09
 * @description https://codeforces.com/contest/2051/problem/E
 */
public class Main {
    /**
     * 思路：
     * 有多少人买取决于 b, 有多少差评取决于 a
     *
     * 出现的价格一定是 a, b中的一个
     * 所以只需要遍历a, b中的价格就可以找到
     * 找 b, 找到 a 中 满足 <= b的有 x, <= a的有 y, 使得 x - y <= k
     * 价格 p = b[i], 买的人有 n - i + 1
     *     价格在  a[l] >= p, n - l + 1
     * 找 a, 找到 b 中 满足 <= a的有 x, <= b的有 y, 使得 x - y <= k
     * 价格 p = a[i], a[i] >= p的人有, n - i + 1
     *    买的人有 b[l] >= p, n - l + 1
     * 上述两个都可以用二分找到
     *
     * 主意 价格相等 计算会少东西
     */
    static FastReader sc = new FastReader();
    static PrintWriter out = new PrintWriter(System.out);
    static int N = 200010;
    static int[] a = new int[N], b = new int[N];
    static int n, k;

    public static void main(String[] args) {
        int T = sc.nextInt();
        while (T-- > 0) {
            n = sc.nextInt();
            k = sc.nextInt();
            for (int i = 1; i <= n; i++) a[i] = sc.nextInt();
            for (int i = 1; i <= n; i++) b[i] = sc.nextInt();
            Arrays.sort(b, 1, n + 1);
            Arrays.sort(a, 1, n + 1);
            long price = 0, num = 0;
            // 遍历 b, 找 a
            for (int i = 1; i <= n; i++) {
                long p = b[i];
                // 价格相同就不需要重复计算, 就会出错
                if (b[i] == b[i - 1]) continue;
                int l = 1, r = n;
                while (l < r) {
                    int mid = l + r >> 1;
                    if (a[mid] >= p) r = mid;
                    else l = mid + 1;
                }
                // 所有人都 < p, 那就说明都在 a[i] < p <= b[i] 之间
                if (a[l] < p) l = n + 1;

                if ((n - i + 1) - (n - l + 1) <= k) {
                    if (price * num < p * (n - i + 1)) {
                        price = p;
                        num = n - i + 1;
                    }
                }
            }
            // 遍历 a, 找 b
            for (int i = 1; i <= n; i++) {
                long p = a[i];
                // 价格相同就不需要重复计算了, 就会出错
                if (a[i] == a[i - 1]) continue;
                int l = 1, r = n;
                while (l < r) {
                    int mid = l + r >> 1;
                    if (b[mid] >= p) r = mid;
                    else l = mid + 1;
                }

                if ((n - l + 1) - (n - i + 1) <= k) {
                    if (price * num < p * (n - l + 1)) {
                        price = p;
                        num = n - l + 1;
                    }
                }
            }
            out.println(price * num);
        }
        out.flush();
    }
}


class FastReader {
    StringTokenizer st;
    BufferedReader br;

    FastReader() {
        br = new BufferedReader(new InputStreamReader(System.in));
    }

    String next() {
        while (st == null || !st.hasMoreElements()) {
            try {
                st = new StringTokenizer(br.readLine());
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return st.nextToken();
    }

    int nextInt() {
        return Integer.parseInt(next());
    }

    String nextLine() {
        String s = "";
        try {
            s = br.readLine();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return s;
    }

    long nextLong() {
        return Long.parseLong(next());
    }

    double nextDouble() {
        return Double.parseDouble(next());
    }

    // 是否由下一个
    boolean hasNext() {
        while (st == null || !st.hasMoreTokens()) {
            try {
                String line = br.readLine();
                if (line == null)
                    return false;
                st = new StringTokenizer(line);
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
        return true;
    }
}
